import { areaList } from '@vant/area-data';
import {getSignHistoryList} from '../../../api/member'
Component({
  data:{
    signRecordList:[],//tab列表
    diffState:[{ color:"#FFD22B", title:"请假"},{color:"#6DD400",title:"签到"},{color:"#FF0B24",title:"旷课"}],
    postData:{
    },//请求体数据
    // 卡片数据
    listData:[],
    selectShow:false,
    currentDate: new Date().getTime(),
    areaList,
    selectDateText:"", //日期时间
    minDate:new Date(1900, 10, 1).getTime(),
    formatter(type:string, value:number) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if(type === 'day'){
        return `${value}日`;
      }
      return value;
    },
    total:0, //总页数
    moreLimt:false,//是否加载完成
    isfreshComplete:false, //是否刷新完成
  },
  methods:{
    onLoad(){
      this.init()
    },
    //  初始化
    init(){
      this.dateFormat()
      const signUserList = getApp().getMemberList().map((val:{stuId:number,userName:string})=>{
       return {
         id:val.stuId,
         label:val.userName
       }
     })
     signUserList.unshift({
       id:0,
       label:"全部"
     })
    // 请求体数据加持
    const dateStr = this.data.selectDateText
    let dateStrList = dateStr.replace('年', ' ').replace('月', '').split(' ');
    let dateResult = dateStrList.map(Number);
     const userId = getApp().getUserInfo().userId
     const postData = {
       year:dateResult[0],
       month:dateResult[1],
       pageNum:1,
       pageSize:10,
       userId
     }
     this.setData({
      postData,
       signRecordList:signUserList
     })
     this.getHistoryList()
    },

    // 获取签到记录列表
    async getHistoryList(){
    const postData = this.data.postData as any
   const {code,data:res} = await getSignHistoryList(postData)
   if(code === 200){
     const {total,list} = res
     const listData = this.data.listData.concat(list)
     this.setData({
      listData,
      total
     })
     
   }
    },
        // 列表刷新课程
        loadPageEvent(data:any){
          const type = data.detail.type
          switch(type){
            case 'refresh':
              this.resetPage()
              this.getHistoryList()
              this.setData({
                isfreshComplete:true
              })
              break;
             case 'load':
              let postData = this.data.postData as any
              if(postData.pageNum * postData.pageSize < this.data.total){
                postData.pageNum  = postData.pageNum + 1
                this.setData({
                  postData
                })
                this.getHistoryList()
              }else{
               this.setData({
                moreLimt: true
               })
              }
              break;
          }
          },
                  // 重置页码
      resetPage(){
        let postData = this.data.postData as any
        postData.pageNum = 1
       this.setData({
        postData,
        listData:[]
       })
      },
    // 选择学员
    handleChange(e:any){
     let postData = this.data.postData as any
    if(e.detail === 0){
     delete postData.stuId
    }else{
      postData.stuId = e.detail
    }
     this.setData({
      postData
     })
     this.resetPage()
     this.getHistoryList()
    },
    // 跳转课程详情
    skipDetail(e:any){
      const {coursename,stuid,serialid} = e.currentTarget.dataset
      
      wx.navigateTo({
        url:`/myCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${coursename}&stuId=${stuid}&serialId=${serialid}`
      })
    },
    //条件筛选选择日期
    selectDate(){
      this.setData({
        selectShow:true
       })   
    },
    // 关闭底部弹框
    onClose(){
     this.setData({
      selectShow:false
     }) 
    },
      // 日期选择改变
      dateSelect(event:any){
        this.setData({
        currentDate: event.detail,
        })
       
      },
         // 确认日期选择
      dateConfirm(e:any){
         this.dateFormat(e.detail)
          this.setData({
           selectShow:false,
          }) 
          const dateStr = this.data.selectDateText
          let dateStrList = dateStr.replace('年', ' ').replace('月', '').split(' ');
          let dateResult = dateStrList.map(Number);
          const postData = this.data.postData as any
          postData.year = dateResult[0]
          postData.month = dateResult[1]
          this.resetPage()
          this.getHistoryList()
      },
      // 日期格式化
      dateFormat(timeCash?:number){
        let year = timeCash ? new Date(timeCash).getFullYear() : new Date().getFullYear()
        let month = timeCash ? new Date(timeCash).getMonth() + 1 : new Date().getMonth() + 1
        this.setData({
          selectDateText: `${year}年${month}月`
        })
       
      }
  }
})